package main;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.ds.druid.DruidDSFactory;
import lombok.SneakyThrows;
import utils.SmallTool;

import javax.sql.DataSource;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author: gcr
 * @Date: 2022-3-21 16:39
 */
public class InsertNewDoi {

    static String insetTable = "compare_doi";

    @SneakyThrows
    public static void main(String[] args) {

        // 2022年3月21日16:39:39 这个主要是把我的doi 和老板的doi 插入表 插入同事存在的先执行我的程序

        SmallTool.printMemory();
        String myDoiSource = "pubmed_vip_extend_new";
//        String bossSource1 = "pmid_doi_down_1_500w";
//        String bossSource2 = "pmid_doi_down_500_1000w";
//        String bossSource3 = "pmid_doi_down_1000_1500w";
//        String bossSource4 = "pmid_doi_down_1500_2000w";
//        String bossSource5 = "pmid_doi_down_2000_2500w";
//        String bossSource6 = "pmid_doi_down_2500_3000w";
        String bossSource7 = "pmid_doi_down_3000_3266w";
        String bossSource8 = "pmid_doi_down_3266_3532w";

        DSFactory.setCurrentDSFactory(new DruidDSFactory());
        DataSource ds3307 = DSFactory.get("server_local3307");
        Db useMe = Db.use();
        Db use3307 = Db.use(ds3307);

        List<String> boosTables = Stream.of(bossSource7, bossSource8).collect(Collectors.toList());

//        String allDate = "select max(pmid) as pmid  from " + myDoiSource;
//        Entity query = useMe.query(allDate).get(0);
//        SmallTool.printMess("最大的id 是" + query.getStr("pmid"));
//
//        Integer count = query.getInt("pmid");
//        int breakSize = 100000;
//        int pageSize = count / breakSize + 1;
//        SmallTool.printMess("全部页码+++++++++" + pageSize);
//
//        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
//        for (int i = 1; i <= pageSize; i++) {
//            SmallTool.printMess("当前页码+++++" + i);
//
//            int start = breakSize * (i - 1) + 1;
//            int end = i * breakSize;
//            SmallTool.printMess("开始id++" + start + "+++结束id++++" + end);
//            String paramSql = "select pmid,doi from " + myDoiSource + " where  pmid between  " + start + " and " + end;
//            List<Entity> querys = useMe.query(paramSql);
//            SmallTool.printMemory();
//
//            querys.parallelStream().forEach(entity -> {
//                Integer pmid = entity.getInt("pmid");
//                String doi = entity.getStr("doi");
//                try {
//                    insertData(pmid, doi, useMe, false);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//
//            });
//        }
        SmallTool.printMess("我的插入完毕 插入老板的doi号码");
        for (String boosTable : boosTables) {
            SmallTool.printMess("开始跑表+++" + boosTable);

            String allDateBoss = "select max(pmid) as pmid  from " + boosTable;
            Entity queryBoss = use3307.query(allDateBoss).get(0);
            SmallTool.printMess("最大的id 是" + queryBoss.getStr("pmid"));

            Integer countBoss = queryBoss.getInt("pmid");
            int breakSizeBoss = 100000;
            int pageSizeBoss = countBoss / breakSizeBoss + 1;
            SmallTool.printMess("全部页码+++++++++" + pageSizeBoss);
//            SmallTool.printMess("即将开始请提前关闭如果想的话");
//            Thread.sleep(10000);
            SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
            for (int i = 1; i <= pageSizeBoss; i++) {
                SmallTool.printMess("当前页码+++++" + i);
                int start = breakSizeBoss * (i - 1) + 1;
                int end = i * breakSizeBoss;
                SmallTool.printMess("开始id++" + start + "+++结束id++++" + end);
                String paramSql = "select pmid,citationDoi from " + boosTable + " where  pmid between  " + start + " and " + end;
                List<Entity> querys = use3307.query(paramSql);
                querys.parallelStream().forEach(entity -> {
                    Integer pmid = entity.getInt("pmid");
                    String doi = entity.getStr("citationDoi");
                    try {
                        insertData(pmid, doi, useMe, true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
            }
        }
    }

    @SneakyThrows
    public static void insertData(Integer pmid, String doi, Db use, boolean isCompareInsert) {
        if (isCompareInsert) {
            // 先比对 有的更新 没有的新插入
            String q = "select * from " + insetTable + " where pmid = ?";
            Entity entity = use.queryOne(q, pmid);
            if (entity == null) {
                use.insert(new Entity().setTableName(insetTable)
                        .set("pmid", pmid)
                        .set("doi_boss", doi)
                );
            } else {
                entity.set("doi_boss", doi);
                Entity where = new Entity();
                where.setTableName(insetTable);
                where.set("id", entity.getInt("id"));
                use.update(entity, where);
            }


        } else {
            use.insert(new Entity().setTableName(insetTable)
                    .set("pmid", pmid)
                    .set("doi_new", doi)
            );
        }


    }
}
